﻿
namespace CQRS.Reports.SqlQueryBuilder
{
    public class SqlDeleteStatementBuilder: ISqlDeleteStatementBuilder
    {
        public string CreateSqlDeleteStatementFromDto<TDto>()
        {
            return string.Format("{0};", GetDeleteString<TDto>());
        }

        public string CreateSqlDeleteStatementFromDto<TDto>(QueryCriteria deleteCriteria) where TDto : Report
        {
            return deleteCriteria != null
                       ? string.Format("{0} {1};", GetDeleteString<TDto>(), GetWhereString(deleteCriteria))
                       : string.Format("{0};", GetDeleteString<TDto>());
        }

        private static string GetDeleteString<TDto>()
        {
            var type = typeof(TDto);
            var tableName = type.Name;

            return string.Format("DELETE FROM {0}", tableName);
        }

        private static string GetWhereString(QueryCriteria deleteCriteria)
        {
            return deleteCriteria.IsNotEmpty
                       ? string.Format("WHERE {0}", string.Join(" AND ", deleteCriteria.GenerateQueryString()))
                       : string.Empty;
        }
    }
}
